package controller;

import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@RestController
public class WebFluxController {
    /**
     * 普通的get
     * @return
     */
    @GetMapping("hello")
    public String hello() {
        return "sdfaafsdfs";
    }

    /**
     * 返回值为json的get
     */
//    @GetMapping("hello1")
//    public User hello1() {
//        User user = new User("1sadf", "asdffdas");
//        return user;
//    }

    /**
     * 无参数的post
     */
    @PostMapping(value = "asdf")
    public String asdf() {
        System.out.println();
        return "dasfadsf";
    }

    /**
     * pathVariable
     */
    @RequestMapping(value = "abc/{name}", params = "myHeader=myValue")
    public String name(@PathVariable String name) {
        return name;
    }
    /**
     * pathVariable
     */
    @GetMapping(value = "players/{name}")
    public String playersName(@PathVariable String name) {
        return name;
    }

//    /**
//     * commit form
//     */
//    @PostMapping(value = "commit", consumes = "application/x-www-form-urlencoded")
//    public User commit(@RequestBody User user){
//
//        return user;
//    }
    @PostMapping(value = "/formS", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
    public Mono<String> save(ServerWebExchange exchange) {
        Mono<MultiValueMap<String, String>> formData =exchange.getFormData();
        return formData.flatMap(map -> {
            System.out.println("formS："+map);
            return Mono.justOrEmpty(map.toString());
        });
    }

    /**
     * header 用来指定headers 如果不匹配则404
     * @return
     */
//    @RequestMapping(value = "json", headers = "myHeader=myValue")
//    public User json(@RequestBody User user) {
//        System.out.println(user);
//        return user;
//    }
    //http://192.168.3.61:8080/pets/42;q=11;r=22
    @GetMapping("/pets/{petId}")
    public String findPet(@PathVariable String petId, @MatrixVariable int q, @MatrixVariable int r) {

        // petId == 42
        // q == 11
        return petId + " " + q + " " + r;
    }

//    @RequestMapping(value = "json/httpEntity")
//    public User httpEntity(HttpEntity<User> entity) {
//        System.out.println(entity);
//        return entity.getBody();
//    }

}
